<?php

include_once "function_xml_basic.php";

$root_path = "/MINI/SECURITY/IDS";

function GetXmlIdsServerInfoList()
{
	global $root_path;
	$query_string = $root_path. "/CONFIG";
        $key_array = array("Enabled", "Warning","Ports", "Dev","Ip","Mask","Number","Syslogd");
        return GetAttributList($query_string, $key_array);
}
/*
$ret_array = GetXmlIdsServerInfoList();
foreach ($ret_array as $i)
{
	print $i["Warning"];
	print $i["Ports"];
}
*/
function SetXmlIdsServerInfo($in_array)
{
        global $root_path;
        $query_string = $root_path. "/CONFIG[1]";

        EditAllAttrOfNode($query_string, $in_array);

	ApplySysIdsConfig();
}

function ApplySysIdsConfig()
{
	global $root_path;
	$command = "/usr/local/snort/bin/snort -de -D -s -N --create-pidfile --nolock-pidfile ";	
	$command_conf = " -c /usr/local/snort/etc/snort.conf ";

	$command .= $command_conf;
	
	$command_kill = 'for i in `ls /var/run/snort*`; do kill -9 `cat $i` ; done; rm -rf /var/run/snort* ;';

	$ret_array = GetXmlIdsServerInfoList();
	$ret = $ret_array[0];
	$host = $ret['Ip']. '/'. $ret['Mask'];
	if ($ret['Ip'] == "")
		$command_host = " -h ". $host. " ";
	else
		$command_host = "";

	$dev = $ret['Dev'];
	if ($dev == "All")
		$command_dev = "";
	else
		$command_dev = " -i ". $dev. " ";

	$port = $ret['Ports'];
	if ($port != "")
		$command_ports = " port ". $port. " ";
	else
		$command_ports = " ";

	$number = $ret['Number'];
	if ($number == 0)
		$command_number = "";
	else
		$command_number = " -n ". $number. " ";
	
	$command_string = $command. $command_host. $command_ports. $command_dev. $command_number;
	
	//print $command_kill;
	//print $command_string;

	shell_exec ($command_kill);

	if ($ret['Enabled'] == 0) // start
	{
		shell_exec ($command_string);
	}

}

?>
